# coding=utf-8
import os
from function_utils import Global, Common

is_systemd = os.path.isdir('/usr/lib/systemd')


def service_status(sysd, sysv):
    if is_systemd:
        cmd = 'systemctl status %s' % sysd
    else:
        cmd = 'service %s status' % sysv
    ret, out, err = Common.execute_cmd(cmd)
    if err:
        print(err)
    return ret


def check_process(proc):
    name, sysd, sysv = proc
    if service_status(sysd, sysv):
        status = '<BLUE> ##### - [x] {0:20}: Normal'.format(name)
    else:
        status = '<RED> ##### - [ ] {0:20}: Failed'.format(name)
    return status


def write_widgets_value(value):
    data = [
        {"MultiInfoText": value}
    ]
    Global.values_write(data)
    Global.report_info(100)


def main():
    status = []
    for p in [('Syslog', 'rsyslog', 'syslog'),
              ('Sshd', 'sshd', 'sshd'),
              ('Crond', 'crond', 'cron'),
              ('Atd', 'atd', 'atd'),
              ('Ntpd', 'ntpd', 'ntp'),
              ('Auditd', 'auditd', 'auditd'),
              ('Haveged', 'haveged', 'haveged')
              ]:
        status.append(check_process(p))
    write_widgets_value('\n'.join(status))


if __name__ == '__main__':
    main()
